<?php
class Silk_Zgb_Block_Product_HomeProducts extends Mage_Catalog_Block_Product_Abstract
{   
	protected $xxsp = 5;
	protected $yl = 6;
	protected $js = 8;
	protected $grxh = 9;
	protected $tl = 10;
	protected $jjry = 11;
	protected $lymm = 13;

	protected $bannerIds = array(4,5,6,7,8,9,10);
	protected $categoryIds = array(5,6,8,9,11,10,13);

	public function getAllIds()
	{
		$cids = explode(',', Mage::getStoreConfig('design/zgb/display_cid', Mage::app()->getStore()));
		$bids = explode(',', Mage::getStoreConfig('design/zgb/display_bid', Mage::app()->getStore()));
		if(count($cids) == count($bids) && !is_null($cids) && !is_null($bids)){
			return array_combine($bids, $cids);
		}
		
		return array_combine($this->bannerIds, $this->categoryIds);
	}

	/*public function getCategoryProducts($id)
	{
		$sql = "SELECT * FROM 
				(SELECT sfoi.`product_id`, cpei.`value` AS brand_id ,SUM(sfoi.`qty_ordered`) AS qty_ordered
				FROM sales_flat_order_item sfoi 
				LEFT JOIN `catalog_product_entity_int` cpei ON sfoi.product_id=cpei.`entity_id` AND attribute_id=139 AND cpei.`store_id`=0
				LEFT JOIN `catalog_product_entity_int` AS `at_status` ON (`at_status`.`entity_id` = sfoi.product_id) AND (`at_status`.`attribute_id` = '96') WHERE at_status.value = '1'
				GROUP BY cpei.`value`) AS sales
				LEFT JOIN `catalog_category_product` ccp ON sales.`product_id`=ccp.`product_id`
				WHERE category_id='{$id}'
				ORDER BY qty_ordered DESC
				";
				
		$connection = Mage::getSingleton('core/resource')->getConnection('core_read');

		return $connection->fetchAll($sql);
	}*/

	public function getCategoryProducts($id)
	{
		$productCollection = Mage::getModel('catalog/category')->load($id)
			->getProductCollection()
			->addAttributeToSelect('*')
		    ->addAttributeToFilter('status', 1)
		    ->addAttributeToFilter('visibility', 4)
		    ->setOrder('position', 'DESC');

		Mage::getSingleton('cataloginventory/stock')
    			->addInStockFilterToCollection($productCollection);
    			
    	$productCollection->distinct('entity_id');
    	return $productCollection->setPage(1, 10);
	}

	public function getCategoryName($id)
	{
		return Mage::getModel('catalog/category')->load($id)->getName();
	}

	public function getCategoryUrl($id)
	{
		return Mage::getModel('catalog/category')->load($id)->getUrl();
	}

	public function getSubcategoryinfo($id)
	{
		$curentCate = Mage::getModel('catalog/category')->load($id);
		if($curentCate->hasChildren())
		{
			$ids = $curentCate->getChildren();
			$subCategories = Mage::getModel('catalog/category')->getCollection();
			$subCategories->getSelect()->where("e.entity_id in ($ids)");
			$subCategories->addAttributeToSelect('name');
			$subCategories->load();
			return $subCategories;
		}else{
			return;
		}
	}

	public function getCategoryBrandinfo($id)
	{
		if($id == $this->xxsp){
			return [
				[
					'brand' => 78,
					'src'   => 'wysiwyg/Brand/leshi.jpg',
				],
				[
					'brand' => 77,
					'src'   => 'wysiwyg/Brand/ao_li_ao1.jpg',
				],
				[
					'brand' => 252,
					'src'   => 'wysiwyg/Brand/miduoqi1.jpg',
				],
				[
					'brand' => 256,
					'src'   => 'wysiwyg/Brand/xufuji1.jpg',
				],
				[
					'brand' => 262,
					'src'   => 'wysiwyg/Brand/zhenqiao_1.jpg',
				],
				[
					'brand' => 45,
					'src'   => 'wysiwyg/Brand/wangwang.jpg',
				],
			];
		}elseif($id == $this->yl){
			return [
				[
					'brand' => 8,
					'src'   => 'wysiwyg/Brand/mengniu.jpg',
				],
				[
					'brand' => 14,
					'src'   => 'wysiwyg/Brand/baishi.jpg',
				],
				[
					'brand' => 76,
					'src'   => 'wysiwyg/Brand/jiaduobao_1.jpg',
				],
				[
					'brand' => 32,
					'src'   => 'wysiwyg/Brand/yili.jpg',
				],
				[
					'brand' => 27,
					'src'   => 'wysiwyg/Brand/kekoukele.jpg',
				],
				[
					'brand' => 74,
					'src'   => 'wysiwyg/Brand/wahaha.jpg',
				],
			];
		}elseif($id == $this->js){
			return [
				[
					'brand' => 82,
					'src'   => 'wysiwyg/Brand/xuehua.jpg',
				],
				[
					'brand' => 7,
					'src'   => 'wysiwyg/Brand/guxiangyang.jpg',
				],
				[
					'brand' => 83,
					'src'   => 'wysiwyg/Brand/qingdaopijiu.jpg',
				],
				[
					'brand' => 225,
					'src'   => 'wysiwyg/Brand/landai.jpg',
				],
				[
					'brand' => 226,
					'src'   => 'wysiwyg/Brand/langjiu.jpg',
				],
				[
					'brand' => 22,
					'src'   => 'wysiwyg/Brand/zhijiang_1.jpg',
				],
			];
		}elseif($id == $this->grxh){
			return [
				[
					'brand' => 91,
					'src'   => 'wysiwyg/Brand/hfs.jpg',
				],
				[
					'brand' => 89,
					'src'   => 'wysiwyg/Brand/piaorou_.jpg',
				],
				[
					'brand' => 106,
					'src'   => 'wysiwyg/Brand/lux1.jpg',
				],
				[
					'brand' => 88,
					'src'   => 'wysiwyg/Brand/jiajieshi.jpg',
				],
				[
					'brand' => 4,
					'src'   => 'wysiwyg/Brand/sfj.jpg',
				],
				[
					'brand' => 186,
					'src'   => 'wysiwyg/Brand/olay1.jpg',
				],
			];
		}elseif($id == $this->jjry){
			return [
				[
					'brand' => 172,
					'src'   => 'wysiwyg/Brand/yujie.jpg',
				],
				[
					'brand' => 108,
					'src'   => 'wysiwyg/Brand/file_5.jpg',
				],
				[
					'brand' => 164,
					'src'   => 'wysiwyg/Brand/lierkang.jpg',
				],
				[
					'brand' => 130,
					'src'   => 'wysiwyg/Brand/baimao.jpg',
				],
				[
					'brand' => 238,
					'src'   => 'wysiwyg/Brand/lvdao.jpg',
				],
				[
					'brand' => 142,
					'src'   => 'wysiwyg/Brand/file_4.jpg',
				],
			];
		}elseif($id == $this->tl){
			return [
				[
					'brand' => 10,
					'src'   => 'wysiwyg/Brand/file_1.jpg',
				],
				[
					'brand' => 108,
					'src'   => 'wysiwyg/Brand/file_5.jpg',
				],
				[
					'brand' => 104,
					'src'   => 'wysiwyg/Brand/file.jpg',
				],
				[
					'brand' => 130,
					'src'   => 'wysiwyg/Brand/file_2.jpg',
				],
				[
					'brand' => 154,
					'src'   => 'wysiwyg/Brand/file_3.jpg',
				],
				[
					'brand' => 142,
					'src'   => 'wysiwyg/Brand/file_4.jpg',
				],
			];
		}elseif($id == $this->lymm){
			return [
				[
					'brand' => 145,
					'src'   => 'wysiwyg/Brand/file_10.jpg',
				],
				[
					'brand' => 144,
					'src'   => 'wysiwyg/Brand/file_11.jpg',
				],
				[
					'brand' => 211,
					'src'   => 'wysiwyg/Brand/yinlu_1.jpg',
				],
				[
					'brand' => 24,
					'src'   => 'wysiwyg/Brand/kangshifu.jpg',
				],
				[
					'brand' => 54,
					'src'   => 'wysiwyg/Brand/tongyi.jpg',
				],
				[
					'brand' => 147,
					'src'   => 'wysiwyg/Brand/file_6.jpg',
				],
			];
		}else{
			return [];
		}
	}

	/*get Home page Left Banners*/
	public function getHomepagebanners($id)
	{
		$storeId = Mage::app()->getStore()->getId();
	    $today = date("Y-m-d");
	    $areaCollection = Mage::getModel('bannerslider/banner')->getCollection()
	    		->addFieldToSelect('*')
	            ->setStoreId($storeId)
	            ->addFieldToFilter('status', 0)
	            ->addFieldToFilter('bannerslider_id', $id)
	            ->addFieldToFilter('start_time', array('lteq' => $today))
	            ->addFieldToFilter('end_time', array('gteq' => $today))
	            ->setOrder('order_banner', "ASC");

	    $areaCollection->getSelect()->join(array('table_alias' => $areaCollection->getTable('bannerslider/value')), 'main_table.banner_id = table_alias.banner_id And table_alias.store_id = '.$storeId);
	    $areaCollection->getSelect()->group("main_table.banner_id");
	    return $areaCollection;
	}
}